package kamikazehero.input;

import java.awt.Graphics;
import java.text.DecimalFormat;

import net.java.games.input.*;
import net.java.games.input.Component.Identifier;


public class InputDevice{

	private Controller controller;
	private EventQueue eventQueue;
	private DecimalFormat format;
	
	private boolean isPaused = false;
	
	public boolean isPaused() {
		return isPaused;
	}

	public void setPaused(boolean isPaused) {
		this.isPaused = isPaused;
	}

	float xPoll = 0.0f;
	float xPrev = 0.0f;
	
	float yPoll = 0.0f;
	float yPrev = 0.0f;
	
	
	public InputDevice(Controller controller){
		this.controller = controller;
	}
	
	public Controller getController() {
		return controller;
	}
	
	public void setController(Controller controller) {
		this.controller = controller;
	}
	
	public void move(Graphics page){
		
		format = new DecimalFormat("0");
		Event event = new Event();
		float prevX = 0.0f;
		float prevY = 0.0f;
		float prevZ = 0.0f;
		
		float testX = 0.0f;
		float testY = 0.0f;
		
		while(!isPaused){
			controller.poll();
			eventQueue = controller.getEventQueue();
			
			while(eventQueue.getNextEvent(event)){
				if(event.getComponent().isAnalog()){
					translateAnalogInput(event);
				}
			}
		}
	}
	
	private void translateAnalogInput(Event event){
		float data;

		data = event.getValue();
		data = this.round(data);
		
		if(event.getComponent().getName().equals("X Axis")){
			xPoll = data;
		}else if(event.getComponent().getName().equals("Y Axis")){
			yPoll = data;
		}
		
		if(xPoll > -0.01f && xPoll < 0.01f){
			//System.out.println("DeadZone");
		}else{
			System.out.println(xPoll);
		}
		xPrev = xPoll;
	}
	
	private float round(float value){
		int scaledValue = (int)(value * 1000.0);
		value = ((float)(scaledValue/1000.0));
		return value;
	}
}
